var express = require('express');
var router = express.Router();
var {
    find,
    insert
} = require('../db')
//登录的路由
router.post('/login', async function (req, res, next) {
    console.log(req.body.mail);
    var result = await find('users', {
        mail: req.body.mail
    })
    console.log(result);
    if (result.length == 0) {
        res.send({
            code: 400,
            msg: '邮箱不存在，请前去注册',
            result
        })
    } else {
        if (req.body.password == result[0].password) {
            res.send({
                code: 200,
                msg: '登录成功',
                result
            })
        } else {
            res.send({
                code: 300,
                msg: '密码或邮箱错误，请重新输入',
                result
            })
        }
    }
});


var {
    sendEmail
} = require('../until/nodemailer');

var json = {
    msg: "成功",
    code: '200',
    data: []
}


// code 全局变量

let code;

// 验证码
router.post('/register/code', async (req, res) => {
    let {
        mail
    } = req.body;
    code = "";
    for (let i = 0; i < 4; i++) {
        var rand = Math.floor(Math.random() * 10); // 取随机数的范围 Math.floor(Math.random()*(max-min+1) + min) 
        code += rand;
    }
    console.log(code); // 随机4位数
    var result = await sendEmail(mail, code);
    if (result) {
        json.msg = "验证码发送成功！"
        json.data = result;
        res.send(json);
    } else {
        json.msg = "验证码发送失败！"
        json.data = result;
        res.send(json);
    }
})


router.post('/register', async (req, res) => {

    var result = await find('users', {
        mail: req.body.mail
    });


    // 判断验证码是否正确
    if (code != req.body.mailcode) {
        json.msg = "验证码输入错误！";
        json.code = 500;
        return res.send(json);
    }


    if (result.length == 0) {
        var data = await insert('users', {
            mail: req.body.mail,
            password: req.body.password
        });
        if (data) {
            res.send({
                code: 200,
                msg: "邮箱名注册成功,请前去登录"
            })
        } else {
            res.send({
                code: 400,
                msg: "邮箱名注册失败,请重新注册"
            })
        }
    } else {
        res.send({
            code: 300,
            msg: "邮箱名已存在,请前去登录"
        })
    }
})




//找回密码
var {
    sendEmail2
} = require('../until/nodemailer2');

router.post('/forget', async function (req, res, next) {
    var result = await find('users', {
        mail: req.body.mail
    });
    console.log(result);
    if (result.length != 0) {
        console.log(1);
        var results = await sendEmail2(req.body.mail, result[0].password);
        if (results) {
            json.msg = "密码发送至您的邮箱"
            json.data = result;
            res.send(json);
        } else {
            json.msg = "验证码发送失败！"
            json.data = result;
            json.code = 300;
            res.send(json);
        }
    }else {
        json.msg = "账号不存在，验证码发送失败！"
        json.data = result;
        json.code = 400;
        res.send(json);
    }

});
module.exports = router;